const merge = require('webpack-merge');
const base = require('./webpack.base.config');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyWebpackPlugin = require('copy-webpack-plugin');
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
const setFilePath = require('./webpack.setFilePath')

module.exports = merge(base,{
  mode:'production',
  devtool:'source-map',
  module:{
    rules:[
      {
        test:/\.styl$/,
        exclude:/node_modules/,
        include:/pages/,
        use:[MiniCssExtractPlugin.loader,'css-loader',
        {
          loader:'postcss-loader',
          options:{
            ident:'postcss',
            plugins:(loader)=>[
              require('autoprefixer')({
                browsers:[
                  '> 1%',
                  'last 5 versions',
                  'not ie <= 8',
                  'ios >= 6',
                  'android >= 4.0'
                ]
              })
            ]
          }
        },'stylus-loader']
      },
      {
        test:/\.css$/,
        exclude:/node_modules/,
        include:/(vendor)/,
        use:['css-loader']
      },
      // {
      //   test:/\.(png|jpg|gif|svg)$/i,
      //   exclude:/node_modules/,
      //   use:[
      //     {
      //       loader:'url-loader',
      //       options:{
      //         limit:300000,
      //       }
      //     }
      //   ]
      // },
      {
        test:/\.html$/,
        exclude:/node_modules/,
        include:/components/,
        use:['html-loader']
      },
      {
        test:/\.(png|jpg|gif|svg)$/i,
        use:[
          {
            loader:'file-loader',
            options:{
              name:'assets/[name].[ext]'
            }
          }
        ]
      },
    ]
  },
  plugins:[
    new CleanWebpackPlugin(['dist/*'],{
      root:base.context
    }),
    new AddAssetHtmlPlugin({ 
      filepath: base.context+'/src/pages/'+setFilePath+'/components/index.js',
      outputPath: '/js',
      publicPath: 'js/'
    }),
    // new AddAssetHtmlPlugin({ 
    //   filepath: base.context+'/src/vendor/jquery-3.3.1.min.js',
    //   outputPath: '/js',
    //   publicPath: 'js/'
    // }),
    new CopyWebpackPlugin([ //支持输入一个数组
      {
        from: base.context+'/src/assets', //将src/assets下的文件
        to: base.context+'/dist/assets' // 复制到publiv
      },
      {
        from: base.context+'/src/common', //将src/assets下的文件
        to: base.context+'/dist/common' // 复制到publiv
      }
    ]),
    new MiniCssExtractPlugin({
      filename:'css/[name].css',
    })
  ]
})